﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace App
{
    public partial class frmCust : Form
    {
        private bool IsQuery = true;
        T_Custom m_Custom = null;
        Entities db = new Entities();
        public string m_custom { get; set; }
        public string custom
        {
            set
            {
                var list = db.T_Custom.Where(a => a.custom == value);
                foreach(var customlist in list)
                {
                    m_Custom = customlist;
                    break;
                }
                if (m_Custom == null) return;
                txtcustom.Text=m_Custom.custom;
                txtcustomFullName.Text=m_Custom.customFullName;
                txtaddress.Text=m_Custom.address;
                cbarea.Text=m_Custom.area;
                txtemail.Text=m_Custom.email;
                cbusdBank.Text=m_Custom.usdBank;
                txtshipMark.Text = m_Custom.shipMark;
                cbcountry.Text=m_Custom.country;
                txthomepage.Text=m_Custom.homepage;
                txtusdAccount.Text=m_Custom.usdAccount;
                cbcity.Text=m_Custom.city;
                txtpost.Text=m_Custom.post;
                txtpartnerStruct.Text=m_Custom.partnerStruct;
                txtphone.Text=m_Custom.phone;
                txttaxno.Text=m_Custom.taxNo;
                txtlcArriveValue.Text=m_Custom.lcArriveValue.ToString();
                txtfax.Text=m_Custom.fax;
                txtbankRoll.Text=m_Custom.bankRoll.ToString();
                txtfob.Text=m_Custom.fob;
                txtpackage.Text=m_Custom.package;
                txtcrediLevel.Text=m_Custom.creditLevel;
                txtcustomValue.Text=m_Custom.customValue;
                txtcustomState.Text=m_Custom.customState;
                txtcoopweWish.Text=m_Custom.cooperWish;
                txtdiscount.Text=m_Custom.disCount.ToString();
                cbfcustomGroup.Text=m_Custom.fcustomGroup;
                txtcustomBuyer.Text=m_Custom.customBuyer;
                txtinsure.Text=m_Custom.insure;
                cbyName.Text=m_Custom.yName;
                cbsName.Text=m_Custom.sName;
                cbzName.Text=m_Custom.zName;
                cbjName.Text=m_Custom.jName;
                cbsGroup.Text=m_Custom.sGroup;
                txtexpiryValue.Text=m_Custom.expiryValue.ToString();
                txtcommission1.Text=m_Custom.commission1;
                txtcommission2.Text=m_Custom.commission2;
                txtyjsc.Text=m_Custom.yjsc.ToString();
                txtshipDateRule.Text=m_Custom.shipDateRule;
            }
        }
        
        public frmCust()
        {
            InitializeComponent();
            gvDataSource();
            panList.BringToFront();
        }
        private void TxtClear()
        {
            Helper.TxtClear(txtcustom);
            Helper.TxtClear(txtcustomFullName);
            Helper.TxtClear(txtaddress);
            Helper.CBClear(cbarea);
            Helper.TxtClear(txtemail);
            Helper.CBClear(cbusdBank);
            Helper.TxtClear(txtshipMark);
            Helper.CBClear(cbcountry);
            Helper.TxtClear(txthomepage);
            Helper.TxtClear(txtusdAccount);
            Helper.CBClear(cbcity);
            Helper.TxtClear(txtpost);
            Helper.TxtClear(txtpartnerStruct);
            Helper.TxtClear(txtphone);
            Helper.TxtClear(txttaxno);
            Helper.TxtClear(txtlcArriveValue);
            Helper.TxtClear(txtfax);
            Helper.TxtClear(txtbankRoll);
            Helper.TxtClear(txtfob);
            Helper.TxtClear(txtpackage);
            Helper.TxtClear(txtcrediLevel);
            Helper.TxtClear(txtcustomValue);
            Helper.CBClear(txtcustomState);
            Helper.TxtClear(txtcoopweWish);
            Helper.TxtClear(txtdiscount);
            Helper.CBClear(cbfcustomGroup);
            Helper.TxtClear(txtcustomBuyer);
            Helper.TxtClear(txtinsure);
            Helper.CBClear(cbyName);
            Helper.CBClear(cbsName);
            Helper.CBClear(cbzName);
            Helper.CBClear(cbjName);
            Helper.CBClear(cbsGroup);
            Helper.TxtClear(txtexpiryValue);
            Helper.TxtClear(txtcommission1);
            Helper.TxtClear(txtcommission2);
            Helper.TxtClear(txtyjsc);
            Helper.TxtClear(txtshipDateRule);
        }
        private void gvDataSource()
        {
            var list = db.T_Custom.ToList();
            bslist.DataSource = list;
            bnavlist.BindingSource = bslist;
            bnav1.BindingSource = bslist;
        }
        private void gvID()
        {
            try
            {
                    if(gvlist.RowCount>0)
                    {
                        int index = gvlist.CurrentRow.Index;
                        custom = gvlist.Rows[index].Cells["colcust"].Value.ToString();
                        m_custom = gvlist.Rows[index].Cells["colcust"].Value.ToString();
                
                    }
                    else
                    {
                        TxtClear();
                    }
            }
            catch (Exception exp)
            {
                Msg.ShowError(exp.Message.ToString()+exp.Source.ToString());
            }
        }
        private void SaveAdd()
        {
            m_Custom = new T_Custom();
            if (db.T_Custom.Count() <= 0) m_Custom.cID = 1;
            else m_Custom.cID = db.T_Custom.OrderByDescending(a => a.cID).First().cID + 1;
            m_Custom.custom=txtcustom.Text ;
            m_Custom.customFullName=txtcustomFullName.Text;
            m_Custom.address=txtaddress.Text;
            m_Custom.area=cbarea.Text;
            m_Custom.email=txtemail.Text;
            m_Custom.usdBank=cbusdBank.Text;
            m_Custom.shipMark=txtshipMark.Text;
            m_Custom.country=cbcountry.Text;
            m_Custom.homepage=txthomepage.Text;
            m_Custom.usdAccount=txtusdAccount.Text;
            m_Custom.city=cbcity.Text;
            m_Custom.post=txtpost.Text;
            m_Custom.partnerStruct=txtpartnerStruct.Text;
            m_Custom.phone=txtphone.Text;
            m_Custom.taxNo=txttaxno.Text;
            if(!string.IsNullOrEmpty(txtlcArriveValue.Text)) m_Custom.lcArriveValue=int.Parse(txtlcArriveValue.Text);
            m_Custom.fax=txtfax.Text;
            if(!string.IsNullOrEmpty(txtbankRoll.Text)) m_Custom.bankRoll=int.Parse(txtbankRoll.Text);
            m_Custom.fob=txtfob.Text;
            m_Custom.package=txtpackage.Text;
            m_Custom.creditLevel=txtcrediLevel.Text;
            m_Custom.customValue=txtcustomValue.Text;
            m_Custom.customState=txtcustomState.Text;
            m_Custom.cooperWish=txtcoopweWish.Text;
            if(!string.IsNullOrEmpty(txtdiscount.Text)) m_Custom.disCount=int.Parse(txtdiscount.Text);
            m_Custom.fcustomGroup=cbfcustomGroup.Text;
            m_Custom.customBuyer=txtcustomBuyer.Text;
            m_Custom.insure=txtinsure.Text;
            m_Custom.yName=cbyName.Text;
            m_Custom.sName=cbsName.Text;
            m_Custom.zName=cbzName.Text;
            m_Custom.jName=cbjName.Text;
            m_Custom.sGroup=cbsGroup.Text;
            if(!string.IsNullOrEmpty(txtexpiryValue.Text)) m_Custom.expiryValue=int.Parse(txtexpiryValue.Text);
            m_Custom.commission1=txtcommission1.Text;
            m_Custom.commission2=txtcommission2.Text;
            if(!string.IsNullOrEmpty(txtyjsc.Text)) m_Custom.yjsc=int.Parse(txtyjsc.Text);
            m_Custom.shipDateRule=txtshipDateRule.Text;
            db.T_Custom.Add(m_Custom);
            int i = db.SaveChanges();
            Msg.ShowInfo(string.Format("已保存{0}条订单信息。", i));
            m_Custom = null;
            this.panList.BringToFront();
            this.btn1.Text = "明细";
            this.IsQuery = true;
        }
        private void SaveModify()
        {
            if (m_Custom == null) return;
            m_Custom.custom = txtcustom.Text;
            m_Custom.customFullName = txtcustomFullName.Text;
            m_Custom.address = txtaddress.Text;
            m_Custom.area = cbarea.Text;
            m_Custom.email = txtemail.Text;
            m_Custom.usdBank = cbusdBank.Text;
            m_Custom.shipMark = txtshipMark.Text;
            m_Custom.country = cbcountry.Text;
            m_Custom.homepage = txthomepage.Text;
            m_Custom.usdAccount = txtusdAccount.Text;
            m_Custom.city = cbcity.Text;
            m_Custom.post = txtpost.Text;
            m_Custom.partnerStruct = txtpartnerStruct.Text;
            m_Custom.phone = txtphone.Text;
            m_Custom.taxNo = txttaxno.Text;
            if (!string.IsNullOrEmpty(txtlcArriveValue.Text)) m_Custom.lcArriveValue = int.Parse(txtlcArriveValue.Text);
            m_Custom.fax = txtfax.Text;
            if (!string.IsNullOrEmpty(txtbankRoll.Text)) m_Custom.bankRoll = int.Parse(txtbankRoll.Text);
            m_Custom.fob = txtfob.Text;
            m_Custom.package = txtpackage.Text;
            m_Custom.creditLevel = txtcrediLevel.Text;
            m_Custom.customValue = txtcustomValue.Text;
            m_Custom.customState = txtcustomState.Text;
            m_Custom.cooperWish = txtcoopweWish.Text;
            if (!string.IsNullOrEmpty(txtdiscount.Text)) m_Custom.disCount = int.Parse(txtdiscount.Text);
            m_Custom.fcustomGroup = cbfcustomGroup.Text;
            m_Custom.customBuyer = txtcustomBuyer.Text;
            m_Custom.insure = txtinsure.Text;
            m_Custom.yName = cbyName.Text;
            m_Custom.sName = cbsName.Text;
            m_Custom.zName = cbzName.Text;
            m_Custom.jName = cbjName.Text;
            m_Custom.sGroup = cbsGroup.Text;
            if (!string.IsNullOrEmpty(txtexpiryValue.Text)) m_Custom.expiryValue = int.Parse(txtexpiryValue.Text);
            m_Custom.commission1 = txtcommission1.Text;
            m_Custom.commission2 = txtcommission2.Text;
            if (!string.IsNullOrEmpty(txtyjsc.Text)) m_Custom.yjsc = int.Parse(txtyjsc.Text);
            m_Custom.shipDateRule = txtshipDateRule.Text;
            int i = db.SaveChanges();
            Msg.ShowInfo(string.Format("已保存{0}条订单信息。", i));
            m_Custom = null;
            this.panList.BringToFront();
            this.btn1.Text = "明细";
            this.IsQuery = true;
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            gvID();
            if(this.IsQuery)
            {
                this.panDetail.BringToFront();
                btn1.Text = "概要";
                this.IsQuery = false;
            }
            else 
            {
                this.panList.BringToFront();
                btn1.Text = "明细";
                this.IsQuery = true;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            m_Custom = null;
            this.panDetail.BringToFront();
            btn1.Text = "概要";
            this.IsQuery = false;
            TxtClear();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            btn1_Click(null, null);
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
                if (Msg.ShowWarning("您确信删除此信息吗？") == System.Windows.Forms.DialogResult.Yes)
                {
                    gvID();
                    if (m_custom == "")
                    {
                        Msg.ShowError("请选择一条记录！");
                        return;
                    }
                    T_Custom outcus = db.T_Custom.First(a => a.custom == m_custom);
                    db.T_Custom.Remove(outcus);
                    int i = db.SaveChanges();
                    Msg.ShowInfo(string.Format("已删除{0}条订单信息。", i));
                    gvDataSource();
                }
            }
            catch(Exception exp)
            {
                Msg.ShowError(exp.Message.ToString()+exp.Source.ToString());
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (Msg.ShowWarning("尚未保存，您确定要取消吗？") == System.Windows.Forms.DialogResult.Yes)
            {
                this.panList.BringToFront();
                this.btn1.Text = "明细";
                this.IsQuery = true;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try 
            {
                if (Msg.ShowWarning("您确信保存订单信息吗？") == System.Windows.Forms.DialogResult.Yes)
                {
                    if (m_Custom == null)
                    {
                        SaveAdd();
                        gvDataSource();
                    }
                    else
                    {
                        SaveModify();
                        gvDataSource();
                    }
                }
            }
            catch(Exception exp)
            {
                Msg.ShowError(exp.Message.ToString()+exp.Source.ToString());
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void gvlist_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                this.custom = gvlist.Rows[e.RowIndex].Cells["colcust"].Value.ToString();
                this.panDetail.BringToFront();
                btn1.Text = "概要";
                this.IsQuery = false;
            }
            catch (Exception)
            {
                
                Msg.ShowError("请选择表里的数据！");
            }
        }

        private void frmCust_Load(object sender, EventArgs e)
        {
            //国家 
            Service.Origin(cbcountry);

            //地区
            Service.Area(cbarea);

            //城市
            Service.DstPost(cbcity);

            Service.Cust(cbfcustomGroup);

            Service.Group(cbsGroup);

            Service.CommandName(cbyName);

            Service.CommandName(cbjName);

            Service.CommandName(cbsName);

            Service.CommandName(cbzName);
        }

        private void cbarea_SelectionChangeCommitted(object sender, EventArgs e)
        {
            var list = db.T_CountryDesc.Where(a => a.area == cbarea.Text).Select(b => b.countryEN).ToList();
            cbcountry.DataSource = list;
            cbcountry.DisplayMember = "countryEN";
            //cbcountry.ValueMember = "countryID";

        }

        private void cbcountry_SelectionChangeCommitted(object sender, EventArgs e)
        {
            var list = db.T_CityDesc.Where(a => a.country == cbcountry.Text).Select(b => b.cityEN).ToList();
            cbcity.DataSource = list;
            cbcity.DisplayMember = "cityEN";
        }
    }
}
